﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ent;
using Travel.Common;
using System.Data.Entity;

namespace Travel.Dal
{
    public class UserInfoDal
    {
        public bool AddUserInfo(UserInfo UserInfo)
        {
            using (var db = new MainDB())
            {
                //IList<UserInfo> i = db.UserInfo.Where(x => x.UserNmae.Contains("a") && x.Sex == "女").OrderByDescending(x => x.Id).ToPageList(1, 5).ToList();
                //IList<UserInfo> info = (from a in db.UserInfo where a.UserNmae.Contains("b") && a.Sex == "女" select a).OrderByDescending(x => x.Id).ToPageList(1, 5).ToList();
                //var ff = (from a in db.UserInfo join b in db.UserDetail on a.UserDetailId equals b.Id select a).FirstOrDefault();
                //var fo = (from a in db.UserInfo.Include("UserDetail") select a).FirstOrDefault();
                var info = db.UserInfo.FirstOrDefault();
                //var inf = from a in db.UserInfo select a;
                db.Database.ExecuteSqlCommand("insert into UserInfo values('ccc','333','女',1)");
                //db.Database.ExecuteSqlCommand("insert into UserDetail values('a@163.com','18658196359')");
                UserInfo f = db.Database.SqlQuery<UserInfo>("select * from UserInfo").FirstOrDefault();
                //db.UserInfo.Add(UserInfo);
                //db.SaveChanges();
                return true;
            }
        }

        public bool SetUserInfo(UserInfo UserInfo)
        {
            using (var db = new MainDB())
            {
                #region 第一种
                //UserInfo info = db.UserInfo.Where(x => x.Id == UserInfo.Id).FirstOrDefault();
                //info.UserNmae = UserInfo.UserNmae;
                //info.Pwd = UserInfo.Pwd;
                //info.Sex = UserInfo.Sex;
                //db.SaveChanges();
                #endregion

                #region 第二种
                //db.Entry(UserInfo).State = EntityState.Modified;
                //db.SaveChanges();
                #endregion

                #region 第三种
                //db.Entry(UserInfo).State = EntityState.Unchanged;
                //db.Entry(UserInfo).Property(m => m.Sex).IsModified = true;

                //var entry = db.Entry(UserInfo);
                //entry.State = EntityState.Unchanged;
                //entry.Property(a => a.Sex).IsModified = true;
                //db.SaveChanges();
                #endregion

                return true;
            }
        }

        public void DeleteUserInfo(int id)
        {
            using (var db = new MainDB())
            {
                //根据Id删除
                db.Entry(new UserInfo { Id = id }).State = EntityState.Deleted;
                db.SaveChanges();
            }
        }

        public void GetUserInfoByPage(int pageIndex, int pageSize)
        {
            using (var db = new MainDB())
            {
                //查询单个字段
                db.UserInfo.OrderByDescending(x => x.Id).ToPageList(pageIndex, pageSize).Select(x => new { UserNmae = x.UserNmae }).ToList().Select(x => new UserInfo { UserNmae = x.UserNmae }).ToList();
            }
        }
    }
}
